package com.gg.registration.hospital.controller;


import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.gg.common.result.CommonResult;
import com.gg.common.result.ResultCodeEnum;
import com.gg.common.utils.MD5;
import com.gg.registration.hospital.service.HospitalSetService;
import com.gg.registration.model.hosp.HospitalSet;
import com.gg.registration.vo.hosp.HospitalSetQueryVo;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import java.util.List;


/**
 * 医院设置的controller
 * 可以完成对医院设置的基本操作，增删改查、分页查询等
 *
 * @CrossOrigin : 最基本的解决跨域问题的注解
 *     跨域问题 有三种方式会产生 :
 *          1、协议不同：http 和 https访问
 *          2、IP地址不同 例如：192.168.150.1 和 172.45.1.1
 *          3、端口号不同：8201 与 9527
 *
 * @author chenjunjia
 * @since 2022-04-02
 */
@RestController
@RequestMapping(value = "/admin/hospitalSet")
//@CrossOrigin
public class HospitalSetController {

    @Resource
    private HospitalSetService service;

    /**
     * 查询医院所有信息 √
     * @return CommonResult 统一封装对象
     */
    @GetMapping("queryall")
    public CommonResult queryAllHospitalSet(){
        List<HospitalSet> list = service.list();
        if (list != null) {
            return new CommonResult(ResultCodeEnum.SUCCESS,list);
        }
        return CommonResult.fail();
    }

    /**
     * 根据 id 删除医院设置 √
     * @param id 医院信息的id
     * @return CommonResult 统一封装对象
     */
    @DeleteMapping("delete/{id}")
    public CommonResult deleteHospitalSetById(@PathVariable("id") Long id){
        boolean flag = service.removeById(id);
        if (flag){
            return CommonResult.ok();
        }
        return CommonResult.fail();
    }

    /**
     * 条件查询带分页 √
     * @param current 当前页
     * @param vo 前端传参实体类
     * @RequestBody 前端用 json 格式传数据，要用Post提交才能得到 requestbody 请求体，前端的json格式用data，对象提交用params
     * @return CommonResult 统一封装对象
     */
    @PostMapping("/pagequery/{current}")
    public CommonResult pageQueryHospitalSet(@PathVariable("current") Long current, @RequestBody(required = false) HospitalSetQueryVo vo){
        Page<HospitalSet> page = new Page(current,5); //传入当前页 以及每页展示条数size
        Page<HospitalSet> pageHospitalSet = service.page(page, service.pageQuery(vo));
//        System.out.println(service.getClass());
        return new CommonResult(ResultCodeEnum.SUCCESS,pageHospitalSet);
    }

    /**
     * 添加医院设置 √
     * 编号不能重复
     *
     * @param hospitalSet 医院设置实体类
     * @return CommonResult 统一封装对象
     */
    @PostMapping("save")
    public CommonResult saveHospitalSet (@RequestBody HospitalSet hospitalSet, HttpServletRequest request){
        hospitalSet.setStatus(1);
        hospitalSet.setSignKey(MD5.randomSignKey());
        //api路径要添加人手动设置才行，因为这个路径是医院的接口
//        hospitalSet.setApiUrl(request.getRequestURL().toString());
        boolean save = service.save(hospitalSet);
        return save ? new CommonResult(ResultCodeEnum.SUCCESS) : CommonResult.fail();
    }

    /**
     * 根据id获取医院设置 √
     * @param id 医院id
     * @return CommonResult 统一封装对象
     */
    @GetMapping("querybyid/{id}")
    public CommonResult queryById(@PathVariable("id") Long id) {
        HospitalSet hospitalSet = service.getById(id);
        if (null != hospitalSet){
            return new CommonResult(ResultCodeEnum.SUCCESS,hospitalSet);
        }
        return CommonResult.fail();
    }

    /**
     * 修改医院设置 √
     * 疑惑：参见 README.txt
     * @param hospitalSet 医院信息，要包含id
     * @return CommonResult 统一封装对象
     */
    @PostMapping("update")
    public CommonResult update(@RequestBody HospitalSet hospitalSet) {
        boolean result = service.updateById(hospitalSet);
        return result ? new CommonResult(ResultCodeEnum.SUCCESS,hospitalSet) : CommonResult.fail();
    }

    /**
     * 批量删除医院设置 √
     * @param id id集
     * @return CommonResult 统一封装对象
     */
    @DeleteMapping("batchdelete")
    public CommonResult batchDeleteHospitalSet(@RequestBody List<Long> id) {
        boolean result = service.removeByIds(id);
        return result ? new CommonResult(ResultCodeEnum.SUCCESS,id) : CommonResult.fail();
    }

    /**
     * 医院设置锁定和解锁
     * @param id 医院id
     * @param status 所需的状态，TODO 1是什么，0是什么
     * @return CommonResult 统一封装对象
     */
    @PutMapping("lockhospitalset/{id}/{status}")
    public CommonResult lockHospitalSet(@PathVariable("id") Long id,
                                  @PathVariable("status") Integer status) {
        //根据id查询医院设置信息
        HospitalSet hospitalSet = service.getById(id);
        //设置状态
        hospitalSet.setStatus(status);
        //调用方法
        service.updateById(hospitalSet);
        return new CommonResult(ResultCodeEnum.SUCCESS,id);
    }

    /**
     * 发送签名短信
     * @param id 医院编号
     * @return CommonResult 统一封装对象
     */
    @PutMapping("sendkey/{id}")
    public CommonResult sendKey(@PathVariable Long id) {
        HospitalSet hospitalSet = service.getById(id);
        String signKey = hospitalSet.getSignKey();
        String hoscode = hospitalSet.getHoscode();

        //TODO 发送短信

        return new CommonResult(ResultCodeEnum.SUCCESS,hospitalSet);
    }
}
